// 导入koa，和koa 1.x不同，在koa2中，我们导入的是一个class，因此用大写的Koa表示:
const Koa = require('koa');

// 注意require('koa-router')返回的是函数:
const router = require('koa-router')();

// 创建一个Koa对象表示web app本身:
const app = new Koa();

const bodyParser = require('koa-bodyparser');
app.use(bodyParser());

// 对于任何请求，app将调用该异步函数处理请求：
// app.use(async (ctx, next) => {
//   await next();
//   ctx.response.type = 'text/html';
//   ctx.response.body = '<h1>hello,koa2</h1>';
// });

// app.use(async (ctx, next) => {
//   console.log(`${ctx.request.method} ${ctx.request.url}`); // 打印URL
//   await next(); // 调用下一个middleware
// });

// app.use(async (ctx, next) => {
//   const start = new Date().getTime(); // 当前时间
//   await next(); // 调用下一个middleware
//   const ms = new Date().getTime() - start; // 耗费时间
//   console.log(`Time: ${ms}ms`); // 打印耗费时间
// });

// app.use(async (ctx, next) => {
//   console.log(ctx);
//   if (ctx.request.path === '/') {
//     ctx.response.body = 'index page';
//   } else {
//     await next();
//   }
// });

// log request URL:
app.use(async (ctx, next) => {
  console.log(`Process ${ctx.request.method} ${ctx.request.url}...`);
  await next();
});

// add url-route:
router.get('/hello/:name', async (ctx, next) => {
  let name = ctx.params.name;
  ctx.response.body = `<h1>hello,${name}</h1>`;
});

// add login:
router.get('/', async (ctx, next) => {
  ctx.response.body = `<h1>Index</h1>
      <form action="/signin" method="post">
          <p>Name: <input name="name" value="koa"></p>
          <p>Password: <input name="password" type="password"></p>
          <p><input type="submit" value="Submit"></p>
      </form>`;
});

//
router.post('/signin', async (ctx, next) => {
  let name = ctx.request.body.name || '',
    password = ctx.request.body.password;
  console.log(`signin with name:${name},password:${password}`);
  if (name === 'a' && password === 'a') {
    ctx.response.body = `<h1>Welcome,${name}!</h1>`;
  } else {
    ctx.response.body = `<h1>Login failed!</h1>
      <p><a href="/">Try again</a></p>`;
  }
});

// add router middleware:
app.use(router.routes());

app.listen(3000);
console.log('app started at port 3000...');
